//
//  LSVideoScreenshotEditViewController.m
//  lightSpot
//
//  Created by lightSpot  on 2022.
//  Copyright © 2021 lightSpot . All rights reserved.
//

#import "LSVideoScreenshotEditViewController.h"

#import "LSVideoScreenshotPreviewView.h"
#import "LSVideoScreenshotEditToolBarView.h"

@interface LSVideoScreenshotEditViewController ()

@property (nonatomic, strong) LSVideoScreenshotPreviewView *gifPreviewView;
@property (nonatomic,strong) UIImage *image;
@property (nonatomic,strong) LSVideoScreenshotEditToolBarView *videoScreenshotEditToolBarView;
@property (nonatomic,assign) float selectedMinimum;//最小选择
@property (nonatomic,assign) float selectedMaximum;//最大选择
@property (nonatomic,assign) NSInteger index;

@end

@implementation LSVideoScreenshotEditViewController

- (void)viewDidLoad {
	[super viewDidLoad];

    [self setNavigationItemTitleViewWithTitle:@"视频截图"];
    [self setRightItemWithTitle:@"保存" selector:@selector(saveButtonAction:)];
    
    if (_images.count > 0) {
        _image = _images[0];
    }
    
    [self.gifPreviewView setData:[LSVideoScreenshotPreviewViewModel ittemModelWithImage:_image] delegate:self];
    [self.view addSubview:self.gifPreviewView];
    
    [self.videoScreenshotEditToolBarView setData:[LSVideoScreenshotEditToolBarViewModel ittemModelWithMini:0 max:_images.count index:_index duration:_duration] delegate:self];
    [self.view addSubview:self.videoScreenshotEditToolBarView];
}

#pragma mark - LSVideoScreenshotEditToolBarViewDelegate 滑动 上一秒 下一秒

//速度
- (void)videoScreenshotEditToolBarView:(LSVideoScreenshotEditToolBarView *)view sender:(UISlider *)sender {
    _index = sender.value;
    _image = _images[_index];
    [self.videoScreenshotEditToolBarView setData:[LSVideoScreenshotEditToolBarViewModel ittemModelWithMini:0 max:_images.count index:_index duration:_duration] delegate:self];
    [self.gifPreviewView setData:[LSVideoScreenshotPreviewViewModel ittemModelWithImage:_image] delegate:self];
}

- (void)videoScreenshotEditToolBarView:(LSVideoScreenshotEditToolBarView *)view button:(UIButton *)sender {
    if (sender.tag == 10) {
        //上一秒
        if (_index == 0) {
            return;
        }
        _index --;
    } else {
        //下一秒
        if (_index + 1 == _images.count) {
            return;
        }
        _index ++;
    }
    _image = _images[_index];
    [self.gifPreviewView setData:[LSVideoScreenshotPreviewViewModel ittemModelWithImage:_image] delegate:self];
    [self.videoScreenshotEditToolBarView setData:[LSVideoScreenshotEditToolBarViewModel ittemModelWithMini:0 max:_images.count index:_index duration:_duration] delegate:self];
}

#pragma mark - Action

//保存
- (void)saveButtonAction:(UIButton *)sender {
    UIImageWriteToSavedPhotosAlbum(_image, self,@selector(image:didFinishSavingWithError:contextInfo:), nil);
}

//保存到相册
- (void)image:(UIImage *)image didFinishSavingWithError:(NSError *)error contextInfo:(void *)contextInfo {
    NSString *msg = nil ;
    if(error){
        msg = @"保存图片失败";
    } else {
        msg = @"保存图片成功";
    }
    [self showProgressHUD:msg];
}

#pragma mark - 懒加载

- (LSVideoScreenshotPreviewView *)gifPreviewView {
    if (!_gifPreviewView) {
        _gifPreviewView = [LSVideoScreenshotPreviewView instanceView];
        _gifPreviewView.frame = CGRectMake(0, self.navigationBarHeight, kScreenWidth, kScreenHeight - self.navigationBarHeight - 141 - 95);
    }
    return _gifPreviewView;
}

//gif设置
- (LSVideoScreenshotEditToolBarView *)videoScreenshotEditToolBarView {
    if (!_videoScreenshotEditToolBarView) {
        _videoScreenshotEditToolBarView = [LSVideoScreenshotEditToolBarView instanceView];
        _videoScreenshotEditToolBarView.frame = CGRectMake(0, kScreenHeight - 145, kScreenWidth, 145);
    }
    return _videoScreenshotEditToolBarView;
}

@end
